package com.qianmi.xsite.render.util;

import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.codec.binary.Base64;
/**
 * @author 杨晓东[of1081]
 * @desc 满减营销活动。
 * @使用场景
 * @company qianmi.com
 * @Date 16/1/25
 **/
public class ZlibUtil {

    private static String ZIPCODE = "UTF-8";

    /**
     * 解压内容::
     * @param content
     * @return
     * @throws IOException
     */
    public static String unGzip(String content) throws IOException {
        InputStream io = new ByteArrayInputStream(Base64.decodeBase64(content));
        GZIPInputStream gzip = new GZIPInputStream(io);
        StringBuffer out = new StringBuffer();
        byte[] b = new byte[1024];
        for (int n; (n = gzip.read(b)) != -1;) {
            out.append(new String(b, 0, n));
        }
        return out.toString();
    }



    public static String gzip(String content) throws IOException {

        ByteArrayOutputStream baostream = new ByteArrayOutputStream();
        OutputStream outStream = new GZIPOutputStream(baostream);
        outStream.write(content.getBytes(ZIPCODE));
        outStream.close();
        return Base64.encodeBase64String(baostream.toByteArray());
    }
}
